package com.ruoyi.standingbook.maintenance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.standingbook.maintenance.domain.Maintenance;
import com.ruoyi.standingbook.maintenance.domain.bo.MaintenanceBo;
import com.ruoyi.standingbook.maintenance.domain.bo.MaintenancePicBo;
import com.ruoyi.standingbook.maintenance.domain.vo.MaintenanceImportDetailVo;
import com.ruoyi.standingbook.maintenance.domain.vo.MaintenancePicVo;
import com.ruoyi.standingbook.maintenance.domain.vo.MaintenanceVo;
import com.ruoyi.standingbook.maintenance.mapper.MaintenanceMapper;
import com.ruoyi.standingbook.maintenance.service.IMaintenanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 其它管养任务完成进度Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class MaintenanceServiceImpl implements IMaintenanceService {

    private final MaintenanceMapper baseMapper;

    /**
     * 查询其它管养任务完成进度
     */
    @Override
    public MaintenanceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询其它管养任务完成进度列表
     */
    @Override
    public TableDataInfo<MaintenanceVo> queryPageList(MaintenanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Maintenance> lqw = buildQueryWrapper(bo);
        Page<MaintenanceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询其它管养任务完成进度列表
     */
    @Override
    public List<MaintenanceVo> queryList(MaintenanceBo bo) {
        LambdaQueryWrapper<Maintenance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Maintenance> buildQueryWrapper(MaintenanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Maintenance> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), Maintenance::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, Maintenance::getServiceType, bo.getServiceType());
        lqw.like(StringUtils.isNotBlank(bo.getMaintenanceName()), Maintenance::getMaintenanceName, bo.getMaintenanceName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), Maintenance::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), Maintenance::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationDescribe()), Maintenance::getLocationDescribe, bo.getLocationDescribe());
        lqw.eq(bo.getDate() != null, Maintenance::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getFrequency()), Maintenance::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, Maintenance::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Maintenance::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), Maintenance::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), Maintenance::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), Maintenance::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), Maintenance::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), Maintenance::getProviderId, bo.getProviderId());

        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
        		Maintenance::getDate ,params.get("beginDate"), params.get("endDate"));

        return lqw;
    }

    /**
     * 新增其它管养任务完成进度
     */
    @Override
    public Boolean insertByBo(MaintenanceBo bo) {
        Maintenance add = BeanUtil.toBean(bo, Maintenance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改其它管养任务完成进度
     */
    @Override
    public Boolean updateByBo(MaintenanceBo bo) {
        Maintenance update = BeanUtil.toBean(bo, Maintenance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Maintenance entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除其它管养任务完成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public Boolean batchInsert(List<MaintenanceImportDetailVo> voList) {
        if (null != voList && !voList.isEmpty()) {
            Date date = voList.get(0).getDate();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            LambdaQueryWrapper<Maintenance> lqw = Wrappers.lambdaQuery();
            lqw.eq(Maintenance::getDate, date);
            this.baseMapper.delete(lqw);
        }

        List<Maintenance> list = BeanCopyUtils.copyList(voList, Maintenance.class);
        list.forEach(e -> {
            e.setId(null);
            e.restBase();
        });
        return this.baseMapper.insertBatch(list);
    }

    @Override
    public List<MaintenancePicVo> getPicByDate(String date) {
        List<MaintenancePicBo> maintenancePicBos = baseMapper.selectPicByDate(date);
        List<MaintenancePicVo> pics = new ArrayList<>();
        maintenancePicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())){
                MaintenancePicVo maintenancePicVo = new MaintenancePicVo();
                maintenancePicVo.setPic(e.getPic1());
                pics.add(maintenancePicVo);
            }
            if (StringUtils.isNotBlank(e.getPic2())){
                MaintenancePicVo maintenancePicVo = new MaintenancePicVo();
                maintenancePicVo.setPic(e.getPic2());
                pics.add(maintenancePicVo);
            }
            if (StringUtils.isNotBlank(e.getPic3())){
                MaintenancePicVo maintenancePicVo = new MaintenancePicVo();
                maintenancePicVo.setPic(e.getPic3());
                pics.add(maintenancePicVo);
            }
            if (StringUtils.isNotBlank(e.getPic4())){
                MaintenancePicVo maintenancePicVo = new MaintenancePicVo();
                maintenancePicVo.setPic(e.getPic4());
                pics.add(maintenancePicVo);
            }
        });
        return pics;
    }

    @Override
    public Integer getSumByDate(String date) {
        return baseMapper.selectSumByDate(date, null, StatusConstant.FINISHED);
    }

    @Override
    public Integer getSumByDateAndCommunity(String date, String community) {
        return baseMapper.selectSumByDate(date, community, StatusConstant.FINISHED);
    }

    @Override
    public Integer getSumByYearAndCommunity(String date, String community) {
        return baseMapper.selectSumByYear(date, community, StatusConstant.FINISHED);
    }

    @Override
    public List<MaintenanceVo> queryListByDate(String date, String community) {
        LambdaQueryWrapper<Maintenance> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(community)) {
            lqw.eq(Maintenance::getCommunity,community);
        }
        lqw.eq(Maintenance::getDate,date);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public Integer getNumByYearToNow() {
        return baseMapper.statisticsNumYearToNow();
    }
}
